$(function() {

	getTeacher();

	var myForm

	function getTeacher() {
		var id = sessionStorage.getItem('id');
		var stamp = new Date().getTime();
		var url = path + "/teacher/getTeacher";
		var param = Object.assign(publicParam, {stamp: stamp}, {id: id})

		$.ajax({
			async: false,
			type: "post",
			url: url,
			dataType: "json",
			data: {param: JSON.stringify(param)},
			headers: {Authorization: sessionStorage.getItem('systoken')},
			beforeSend: function() {loading("数据加载中，请稍后......");},
			success: function(data) {
				
				var data = data.rows[0];
				$(".name").val(data.name);
				$(".account").val(data.account);
				$(".birthday").val(data.birthday);
				$(".age").val(data.age);
				$(".credentialsTime").val(data.credentialsTime);
				/*$(".school").val(data.school);*/
				$(".teachingQualifications").val(data.teachingQualifications); //授课资质
				$(".educationHighest").val(data.educationHighest); //最高学历	
				$(".personNumber").val(data.personNumber); //人员编号
				$(".undoutedlyTime").val(data.undoutedlyTime); //评聘时间
				$(".unit").val(data.unit); //所在单位
				//$(".password").val(data.password) //密码
				$(".idCard").val(data.idCard);
				//$(".qrCode").attr("src", qrCode);
				//$(".gender").val(gender);
				$(".liaison").val(data.liaison);
				$(".address").val(data.address);
				$(".email").val(data.email);
				$(".englishLevel").val(data.englishLevel);
				$(".rank").val(data.rank);
				$(".preViewImage").attr("src", data.head);
				$(".teachingQualifications").val(data.teachingQualifications);
				
				if(data.gender == 1) {
					$("[name ='gender']:eq(0)").attr("checked", false);
					$("[name ='gender']:eq(1)").attr("checked", true);
				}
				if(data.gender == 0) {
					$("[name ='gender']:eq(1)").attr("checked", false);
					$("[name ='gender']:eq(0)").attr("checked", true);
				}

				var category = data.category; 
				var profession = data.profession;	

				//类别判断
				if(data.category == 0) {
					$(".category").val("内部");
				} else if(data.category == 1) {
					$(".category").val("外部");
				} else if(data.category == 2) {
					$(".category").val("特聘");
				} else if(data.category == 3) {
					$(".category").val("专职");
				}

				//专业判断
				if(data.profession == 0) {
					$(".profession").val("管制");
				} else if(data.profession == 1) {
					$(".profession").val("气象");
				} else if(data.profession == 2) {
					$(".profession").val("通导");
				}
				
				$(".category").find("option[value = '"+category+"']").attr("selected","selected");
				$(".profession").find("option[value = '"+profession+"']").attr("selected","selected");
			},
			complete: function () {
				layui.use(['form', 'laydate'], function() {
					var form = layui.form;
					form.render('select');
					form.render('radio');

					layer.closeAll('dialog'); //关闭信息框
				})
			},
			error: function(data) {
				layer.msg("请重新登录")
				location.href = "../../login.html"
			}
		});

	}

	function loading(msg) {
		layui.use(['form', 'laydate'], function() {
			layer.msg(msg, {
				icon: 16,
				shade: [0.1, '#fff'],
				area:['240px', '65px']
				/*time: true*/ //不自动关闭
			})
		})
	}
	layui.use(['form', 'laydate'], function() {
		var form = layui.form;
		myForm = form;
		var laydate = layui.laydate;
		laydate.render({
			elem: '.birthday'
		});

		$(".close").click(function() {
			closeIframe();
		})

		laydate.render({
			elem: '#undoutedlyTime'
		});

		$("#personInfo-wrapper").validate({
			onfocusout: function(element) {
				$(element).valid();
			},
			rules: {
				password: {
					minlength: 5
				},
				confirm_password: {
					minlength: 5,
					equalTo: "#password"
				}
			},
			messages: {
				password: {
					required: "请输入密码",
					minlength: "密码长度不能小于 5 个字母"
				},
				confirm_password: {
					required: "请输入密码",
					minlength: "密码长度不能小于 5 个字母",
					equalTo: "两次密码输入不一致"
				}
			}
		});

		//监听完善个人信息提交
		form.on('submit(pullInfo)', function(data) {
			var paramInfo = data.field;
			var stamp = new Date().getTime();
			var src = $('.preViewImage').attr('src');
			var category = $(".category option:selected").val();
			var profession = $(".profession option:selected").val();
			var personNumber = $(".personNumber").val();
			var teachingQualifications = $(".teachingQualifications").val();
			var credentialsTime = $(".credentialsTime").val();
			var passwords = $(".password").val()
			var param = Object.assign(paramInfo, publicParam, {
				stamp: stamp,
				category: category,
				profession: profession,
				personNumber: personNumber,
				teachingQualifications: teachingQualifications,
				credentialsTime: credentialsTime,
				passwords: passwords,
				id: sessionStorage.getItem('id')
			}, {
				head: src
			});
			var url = path + "/teacher/updateTeacher";
			$.ajax({
				url: url,
				data: {param: JSON.stringify(param)},
				dataType: 'json',
				type: 'post',
				async: true,
				headers: {Authorization: sessionStorage.getItem('systoken')},
				success: function(data) {
					if(data.status == '000') {
						alert("提交个人信息成功")
						parent.layer.closeAll();
					} else {
						layer.msg("网络异常!请稍后再回来....")
					}
				}
			})
			return false;
		});

		//			监听修改个人信息事件
		form.on('submit(updateInfo)', function(data) {
			var paramInfo = data.field;
			var stamp = new Date().getTime();
			var pass = $(".finalPass").val();
			var param = Object.assign(paramInfo, publicParam, {
				stamp: stamp,
				id: sessionStorage.getItem('id'),
				password: pass
			});
			var url = path + "/student/updateStudent";
			$.ajax({
				url: url,
				data: {param: JSON.stringify(param)},
				dataType: 'json',
				type: 'post',
				async: true,
				headers: {Authorization: sessionStorage.getItem('systoken')},
				success: function(data) {
					if(data.status == '000') {
						layer.msg("更新个人信息成功");
						closeIframe();
					} else {
						layer.msg("网络异常!请稍后再回来....")
					}
				}
			})
			return false;
		});

		form.verify({
			account: function(value, item) { //value：表单的值、item：表单的DOM对象
				if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
					return '用户名不能有特殊字符';
				}
				if(/(^\_)|(\__)|(\_+$)/.test(value)) {
					return '用户名首尾不能出现下划线\'_\'';
				}
				if(/^\d+\d+\d$/.test(value)) {
					return '用户名不能全为数字';
				}
				if(value == "") {
					return '用户名不能为空'
				}
			},
			/*pass: [
				/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
			],*/
			pass: function(value) {
				if(value != '') {
					if(!/^[\S]{6,12}$/.test(value)) {
						return '密码必须6到12位，且不能出现空格'
					}
				}
			},
//			personNumber: function(value) {
//				if(value == "") {
//					return "人员编号为必填项"
//				}
//			},
			credentialsTime: function(value) {
				if(value != '') {
					if(!/^\d+$/.test(value)) {
						return '只能填写数字或者不填';
					}
				}
			}
		});

	});
});
sessionStorage.removeItem("menu");
sessionStorage.removeItem("curMenu");